package br.ufpe.cin.rdfilter.util;

import java.util.ArrayList;
import java.util.Iterator;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.sparql.core.TriplePath;
import com.hp.hpl.jena.sparql.syntax.ElementPathBlock;
import com.hp.hpl.jena.sparql.syntax.ElementVisitorBase;

/**
 * 
 * @author Alberto
 * This class get all resources in the Graph Patterns on SPARQL queries.
 * Each resource is stored and ordered in a list. 
 *
 */
public class ElementVisitorResources extends ElementVisitorBase{

	private ArrayList<String> visitedResources;
	
	public ElementVisitorResources(){
		this.visitedResources = new ArrayList<String>();
	}
	
	public ArrayList<String> getVisitedResources() {
		return visitedResources;
	}
	
	public void visit(ElementPathBlock el) {
		
		Node subject, object;
		
		Iterator<TriplePath> iterator = el.getPattern().iterator();
		while (iterator.hasNext()) {
			TriplePath triplePath = iterator.next();
			
			subject = triplePath.getSubject();
			object = triplePath.getObject();
			
			// only if the subject is a URI, it will inserted into the list
			if(subject.isURI()){
				visitedResources.add(subject.toString());
			}
			
			// only if the object is a URI, it will inserted into the list
			if(object.isURI()){
				visitedResources.add(object.toString());
			}
			
		}
		super.visit(el);
	}
	
	
	
}
